<template>
  <div>
    <!-- show-checkbox:可选择的 -->
    <!-- default-expand-all：默认展开所有节点 -->
    <!-- check-strictly：父子节点不关联 -->
    <el-tree ref="promissionDetail" node-key="id" show-checkbox default-expand-all check-strictly :data="list" :props="{label:'name'}" />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="$emit('close')">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import { assignPerm, promissDetail } from '@/api/roles'
export default {
  name: 'VueAdminTemplateAssignPermission',
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPromissions()
    this.loadPromissDetail()
  },
  mounted() {

  },

  methods: {
    // 获取角色列表
    async loadPromissions() {
      const res = await getPermissionList().catch(e => e)
      if (!res.success) return
      // console.log(res)
      this.list = tranListToTreeData(res.data)
    },
    // 根据id获取权限点详情
    async loadPromissDetail() {
      const res = await promissDetail(this.roleId).catch(e => e)
      if (!res.success) return
      // console.log(res.data.permIds)
      // setCheckedKeys数据回写
      this.$refs.promissionDetail.setCheckedKeys(res.data.permIds)
    },
    // 点击确定，更新角色的权限点
    async hSubmit() {
      // getCheckedKeys是element内部封装的方法，可以获取点击的多选框
      const permIds = this.$refs.promissionDetail.getCheckedKeys()
      // console.log(result)
      const res = await assignPerm({ id: this.roleId, permIds }).catch(e => e)
      if (!res.success) return this.$message.error(res.message)
      this.$message.success(res.message)
      // 关闭dialog
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
